package com.cakebackoffice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView splashScreen = (ImageView) findViewById(R.id.splash_image);

        splashScreen.setOnClickListener(monListenner);

        /*
Salut je vais bourrer de la base de donnée   *

        ProductType productType = new ProductType("gatobonbon", R.drawable.gatobonbon, "GatoSucré");
        productType.saveToDatabase(this);
        Product pouet = new Product(productType);
        pouet.saveToDatabase(this);
        ArrayList<UnitarySale> liste = new ArrayList<UnitarySale>();
        liste.add(new UnitarySale(33f, 19f, pouet.getType(), pouet));
        liste.add(new UnitarySale(33f, 19f, pouet.getType(), pouet));
        liste.add(new UnitarySale(33f, 19f, pouet.getType(), pouet));
        liste.add(new UnitarySale(33f, 19f, pouet.getType(), pouet));

        Order order = new Order(
                new Client("Cotroeeeee", "LeSaleGros", "069227463322"),
                new Address("41 bd Vauban", "Pouet Pouet", "Lille", "59000"),
                liste,
                new Date(1990, 12, 28)

        );

        for (UnitarySale u : liste) {
            u.saveToDatabase(this);
            u.getProduct().saveToDatabase(this);
        }
        order.getClient().saveToDatabase(this);
        order.getDeliveryPlace().saveToDatabase(this);
        order.saveToDatabase(this);

        /*
       Salut j'ai fini de bourrer de la base de donnée
        */

        new InitialisationTask(this).execute(0);
    }


    private class InitialisationTask extends SyncTaskManager.ApplicationInitialisation {

        public InitialisationTask(Context context) {

            super(context);
        }

        @Override
        protected void onPostExecute(Integer i) {
            super.onPostExecute(i);
            if (i > 0) {
                HelloToast();
                StartOrderActivity();
            }
        }
    }

    public View.OnClickListener monListenner = new View.OnClickListener() {
        public void onClick(View v) {
            HelloToast();
            StartOrderActivity();
        }
    };


    private void HelloToast() {
        Context context = getApplicationContext();
        CharSequence text = "Welcome to Cake@Home !";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

    private void StartOrderActivity() {
        Intent intent = new Intent(this, OrderActivity.class);
        startActivity(intent);
        finish();
    }
}
